package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.common.ThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;
    /**
     * 素材图片上传
     * @param multipartFile
     * @return
     */
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        //1.判断参数是否为空，是否合法
        if (multipartFile.isEmpty() || multipartFile.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.生成文件名称
        String fileNamePrefix = UUID.randomUUID().toString().replace("-", "");
        String originalFilename = multipartFile.getOriginalFilename();
        int lastIndexOf = originalFilename.lastIndexOf(".");
        String fileNamePostfix = originalFilename.substring(lastIndexOf);
        String fileNameFull = fileNamePrefix = fileNamePostfix;
        try {
            //3.把文件放入到minion中
            String url = fileStorageService.uploadImgFile("", fileNameFull, multipartFile.getInputStream());
            //4.构建数据保存
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUserId(ThreadLocalUtil.getUserId());
            wmMaterial.setUrl(url);
            wmMaterial.setType(0);
            wmMaterial.setIsCollection(0);
            wmMaterial.setCreatedTime(new Date());
            this.save(wmMaterial);
            //5.响应素材（目的是为了做回显效果）
            return ResponseResult.okResult(wmMaterial);
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR,"文件上传失败");
        }
    }

    /**
     * 素材图片分页列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult list(WmMaterialDto dto) {
        //1.设置参数默认值
        dto.checkParam();
        //2.多条件查询
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmMaterial::getUserId,ThreadLocalUtil.getUserId());
        if (dto.getIsCollection() > 0){ //收藏
            wrapper.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        wrapper.orderByDesc(WmMaterial::getCreatedTime);
        //3.page固定方法
        IPage<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        this.page(page,wrapper);
        //4.构建分页响应结果
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }
}
